/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginForm.java
 *
 * Created on Nov 8, 2012, 11:55:06 PM
 */
package Form;

import Boundary.Presensi;
// import Boundary.User;
import Boundary.User;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Fave
 */
public class LoginForm extends javax.swing.JFrame {

    /** Creates new form LoginForm */
    public LoginForm() {
        initComponents();
        GregorianCalendar g = new GregorianCalendar();
        tanggal.setDate(g.getTime());
        tanggal.setVisible(false);
        setTitle("Sistem Presensi Akademik SMS Gateway");

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        passwordTextField = new javax.swing.JPasswordField();
        Loginbutton = new javax.swing.JButton();
        Lihat = new javax.swing.JButton();
        tanggal = new com.toedter.calendar.JDateChooser();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/logo.jpg"))); // NOI18N

        jLabel4.setFont(new java.awt.Font("Rockwell", 1, 18));
        jLabel4.setText("SMA NEGERI 1 PARAKAN");

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Login "));

        jLabel1.setFont(new java.awt.Font("Rockwell", 1, 14));
        jLabel1.setText("USERNAME ");

        usernameTextField.setToolTipText("Masukkan Username");
        usernameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usernameTextFieldActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Rockwell", 1, 14)); // NOI18N
        jLabel2.setText("PASSWORD");

        passwordTextField.setToolTipText("Masukkan Password");
        passwordTextField.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        Loginbutton.setText("Login");
        Loginbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginbuttonActionPerformed(evt);
            }
        });

        Lihat.setText("Lihat");
        Lihat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LihatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(Loginbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Lihat, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(passwordTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addComponent(usernameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(26, 26, 26)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(57, 57, 57)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Loginbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Lihat, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(174, 174, 174)
                        .addComponent(tanggal, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(505, 505, 505))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(tanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(11, 11, 11)
                        .addComponent(jLabel4)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 595, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void usernameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usernameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_usernameTextFieldActionPerformed

    private void LoginbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginbuttonActionPerformed

        String user = usernameTextField.getText();
        String pass = passwordTextField.getText();
        User u = new User();
        u.setUsername(user);
        u.setPassword(pass);
        Presensi p = new Presensi();
//         User u = new User();


        if (user.equals("")) {
            JOptionPane.showMessageDialog(null, "Username belum diisi", "Pesan", JOptionPane.INFORMATION_MESSAGE);
        } else if (pass.equals("")) {
            JOptionPane.showMessageDialog(null, "Password belum diisi", "Pesan", JOptionPane.INFORMATION_MESSAGE);
        } else if (user.equals("guru") && pass.equals("guru")) {
            JOptionPane.showMessageDialog(null, "Selamat datang ", "Pesan", JOptionPane.INFORMATION_MESSAGE);
            GuruForm g = new GuruForm();
            g.setVisible(true);
              usernameTextField.setText("");
                    passwordTextField.setText("");
        } else if (user.equals("tu") && pass.equals("tu")) {
            JOptionPane.showMessageDialog(null, "Selamat datang ", "Pesan", JOptionPane.INFORMATION_MESSAGE);
            TUForm t = new TUForm();
            t.setVisible(true);
              usernameTextField.setText("");
                    passwordTextField.setText("");
        } else {

            try {

                if (LoginControl.getKoneksi().cariSiswa(user, pass) != null) {
//                   JOptionPane.showMessageDialog(null, "Anda berhasil presensi", "Pesan", JOptionPane.INFORMATION_MESSAGE);

                    Date tgl = this.tanggal.getDate();
                    SimpleDateFormat tglsimple = new SimpleDateFormat("dd-MM-yyyy");
                    String tanggal = tglsimple.format(tgl);
                    p.setTanggal(tanggal);
                    u.setUsername(user);
                    p.setNis(u);
                    p.setKodekehadiran(1);

                    try {
                    LoginControl.getKoneksi().update(p);
                    JOptionPane.showMessageDialog(rootPane, "Anda telah melakukan presensi", "Pesan", JOptionPane.INFORMATION_MESSAGE);
//                            LoginForm f = new LoginForm();
//                            f.setVisible(true);

                    usernameTextField.setText("");
                    passwordTextField.setText("");
                    } catch (SQLException ex) {
                        Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);
//                        JOptionPane.showMessageDialog(rootPane, "gagal masuk");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Cek Username dan Password anda salah ", "Pesan", JOptionPane.INFORMATION_MESSAGE);
                }

//                if (user != null && pass !=null) {
//                System.out.println("sdsf");}
            } catch (SQLException ex) {
                // Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);
                //   System.out.println("lonte");
            }
//
//            try {
////
////                    if (u != null) {
////                   System.out.println(u. getUsername());
////               }
////             if(sds != null) {
////                 System.out.println(sds.getUsername());
////             }
////             } else {
//
////                u.setStatus(LoginControl.getKoneksi().cariStatus(u));
//                 // LoginControl.getKoneksi().cariSiswa(u);
//
//
//                JOptionPane.showMessageDialog(rootPane, "belsdsds");
//                 // System.out.println(u.getUsername());
//                 // System.out.println(u.getPassword());
//
////                   Presensi p = new Presensi();
////
////                    Date tgl = this.tanggal.getDate();
////                    SimpleDateFormat tglsimple = new SimpleDateFormat("dd-MMM-yy");
////                    String tanggal = tglsimple.format(tgl);
////                    p.setTanggal(tanggal);
////                    u.setUsername(user);
////                    p.setNis(u);
////                    p.setKodekehadiran(1);
////
////
////                    try {
////                   

        }
    }//GEN-LAST:event_LoginbuttonActionPerformed

    private void LihatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LihatActionPerformed
        SiswaLihatPresensi s = new SiswaLihatPresensi();
        s.setVisible(true);
    }//GEN-LAST:event_LihatActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //            UIManager.setLookAndFeel(new WindowsClassicLookAndFeel());
                new LoginForm().setVisible(true);


            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Lihat;
    private javax.swing.JButton Loginbutton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPasswordField passwordTextField;
    private com.toedter.calendar.JDateChooser tanggal;
    private javax.swing.JTextField usernameTextField;
    // End of variables declaration//GEN-END:variables
}
